package uit.edu.vn.Utility;

import uit.edu.vn.Activity.LoginActivity;
import uit.edu.vn.Activity.MainActivity;
import uit.edu.vn.Model.FileObject;
import uit.edu.vn.Model.UserObject;
import uit.edu.vn.Utility.MyActivity;

import java.util.ArrayList;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;

import android.content.Context;
import android.content.SharedPreferences;

public class WebService {
//		private static String SOAP_ACTION = "http://backupmanager.jelastic.servint.net/ServerWeb/services/Service/";
//		private static String URL  = 		"http://backupmanager.jelastic.servint.net/ServerWeb/services/Service?wsdl";	
		private static String Address = null;
//		private static String SOAP_ACTION = "http://"+ Address +"/ServerWeb/services/Service/";
//		private static String URL = 		"http://"+ Address +"/ServerWeb/services/Service?wsdl";
//	    private static final String STRING_NAME_USERSETTING = "USER REFERENCES";
//	    private SharedPreferences ShareRef = null;
		private static String NAMESPACE = 	"http://vn.edu.uit";  		
	     
		// Kiem tra thong tin dang nhap cua  nguoi dung tren webserver bang webservices
        public static String checkLogin(String username, String password) throws Exception {

        	Address = LoginActivity.getIPAddress();
    		String SOAP_ACTION = "http://"+ Address +"/ServerWeb/services/Service/";
    		String URL = 		"http://"+ Address +"/ServerWeb/services/Service?wsdl";
            String METHOD_NAME = "CheckLogin";
            SOAP_ACTION = SOAP_ACTION + METHOD_NAME;            
                      
        	System.out.println("CheckLogin Address : " + Address);
        	System.out.println("CheckLogin SOAP_ACTION : " + SOAP_ACTION);
        	System.out.println("CheckLogin URL : " + URL);
            
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            request.addProperty("username", username);
            request.addProperty("password", password);
                                    
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.setOutputSoapObject(request);
            AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);              
            androidHttpTransport.call(SOAP_ACTION, envelope);                               
            SoapPrimitive result = (SoapPrimitive) envelope.getResponse();          
            String myResult = result.toString().trim();
            return myResult;
        }


        // Tao tai khoan moi cho nguoi dung
        public static String createAccount(String username, String password,String email) throws Exception {

        	Address = LoginActivity.getIPAddress();
            String METHOD_NAME = "CreateAccount";
    		String SOAP_ACTION = "http://"+ Address +"/ServerWeb/services/Service/";
    		String URL = 		"http://"+ Address +"/ServerWeb/services/Service?wsdl";
            SOAP_ACTION = SOAP_ACTION + METHOD_NAME;
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            
        	System.out.println("CreateAccount Address : " + Address);
        	System.out.println("CreateAccount SOAP_ACTION : " + SOAP_ACTION);
        	System.out.println("CreateAccount URL : " + URL);

            request.addProperty("username", username);
            request.addProperty("password", password);
            request.addProperty("email", email);
            request.addProperty("note", "");
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);         
            envelope.setOutputSoapObject(request);
            AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);

            androidHttpTransport.call(SOAP_ACTION, envelope);
            SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
            String myResult = result.toString().trim();
            return myResult;
        }
        
        public static UserObject getAllInfor(int userid)throws Exception{

        	Address = MainActivity.address.toString();
            String METHOD_NAME = "GetInfor";
    		String SOAP_ACTION = "http://"+ Address +"/ServerWeb/services/Service/";
    		String URL = 		"http://"+ Address +"/ServerWeb/services/Service?wsdl";
            SOAP_ACTION = SOAP_ACTION + METHOD_NAME;
            
            
        	System.out.println("GetInfor Address : " + Address);
        	System.out.println("GetInfor SOAP_ACTION : " + SOAP_ACTION);
        	System.out.println("GetInfor URL : " + URL);        	
        	
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            request.addProperty("userid", userid);
            SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);         
            soapEnvelope.setOutputSoapObject(request);
            
            AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
            androidHttpTransport.call(SOAP_ACTION, soapEnvelope);
                        
            try{
            	UserObject user = new UserObject();
        		SoapObject resultsRequestSOAP = (SoapObject) soapEnvelope.bodyIn;   		
        		SoapObject sObject = (SoapObject) resultsRequestSOAP.getProperty(0);
        		user.setDayRegistry(sObject.getProperty("dayRegistry").toString().trim());
        		user.setSumCapacity(sObject.getProperty("sumCapacity").toString().trim());
        		user.setRemainCapacity(sObject.getProperty("remainCapacity").toString().trim());
        		user.setNumContact(sObject.getProperty("numContact").toString().trim());
        		user.setNumSMS(sObject.getProperty("numSMS").toString().trim());
        		user.setNumOther(sObject.getProperty("numOther").toString().trim());
        		return user;

            }catch(Exception ex){
            	ex.printStackTrace();
            }                        
            return null;        	        	
        }             
        
        
        public static ArrayList<FileObject> getAllList(int userid, int Type)throws Exception{

          Address = MainActivity.address.toString();
          String METHOD_NAME = "getListFile";
  		  String SOAP_ACTION = "http://"+ Address +"/ServerWeb/services/Service/";
  		  String URL = 		"http://"+ Address +"/ServerWeb/services/Service?wsdl";          
          SOAP_ACTION = SOAP_ACTION + METHOD_NAME;
          
      	  System.out.println("getListFile Address : " + Address);
      	  System.out.println("getListFile SOAP_ACTION : " + SOAP_ACTION);
      	  System.out.println("getListFile URL : " + URL);    
      	
          SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
          request.addProperty("userID", userid);
          request.addProperty("type", Type);
          SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);         
          soapEnvelope.setOutputSoapObject(request);
          
          AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
          androidHttpTransport.call(SOAP_ACTION, soapEnvelope);

          try{
      		SoapObject resultsRequestSOAP = (SoapObject) soapEnvelope.bodyIn;   		
      		int numberOfItem = resultsRequestSOAP.getPropertyCount();        		
      		ArrayList<FileObject> listFile = new  ArrayList<FileObject>();
              for(int i = 0; i < numberOfItem; i++){
      			SoapObject sObject = (SoapObject) resultsRequestSOAP.getProperty(i);
      			FileObject f = new FileObject() {};
  				f.setFileID(Integer.parseInt(sObject.getProperty("fileID").toString()));
  				f.setUserID(Integer.parseInt(sObject.getProperty("userID").toString()));
  				f.setFileName(sObject.getProperty("fileName").toString().trim());
  				f.setFileType(Integer.parseInt(sObject.getProperty("fileType").toString()));
  				f.setFileSize(Integer.parseInt(sObject.getProperty("fileSize").toString()));
  				f.setPathFile(sObject.getProperty("pathFile").toString().trim());
  				f.setStatusFile(Integer.parseInt(sObject.getProperty("statusFile").toString()));
  				f.setNote(sObject.getProperty("note").toString().trim());
  				f.setDayBackup(sObject.getProperty("dayBackup").toString());   				
      			listFile.add(f);
              }
              return listFile; 
          }catch(Exception ex){
          	ex.printStackTrace();
          }
          return null;
      }
        
        
        // doi password cua nguoi dung, gui yeu cau voi userid va password moi, nhan chuoi tra ve
        //neu -1 that bai, != -1 thanh cong
        public static String ChangePassword(int userid, String newPass) throws Exception {

        	Address = MainActivity.address.toString();
            String METHOD_NAME = "ChangePassword";
    		String SOAP_ACTION = "http://"+ Address +"/ServerWeb/services/Service/";
    		String URL = 		"http://"+ Address +"/ServerWeb/services/Service?wsdl";
            SOAP_ACTION = SOAP_ACTION + METHOD_NAME;
            
          	System.out.println("ChangePassword Address : " + Address);
          	System.out.println("ChangePassword SOAP_ACTION : " + SOAP_ACTION);
          	System.out.println("ChangePassword URL : " + URL);
            
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            request.addProperty("userid", userid);
            request.addProperty("newPass", newPass);

            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.setOutputSoapObject(request);
            AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);              
            androidHttpTransport.call(SOAP_ACTION, envelope);                               
            SoapPrimitive result = (SoapPrimitive) envelope.getResponse();          
            String myResult = result.toString().trim();
            return myResult;
        }
        
        
        public static String deleteFileById(int fileId, String comment) throws Exception {

        	Address = MainActivity.address.toString();
            String METHOD_NAME = "deleteFileById";
    		String SOAP_ACTION = "http://"+ Address +"/ServerWeb/services/Service/";
    		String URL = 		"http://"+ Address +"/ServerWeb/services/Service?wsdl";
            SOAP_ACTION = SOAP_ACTION + METHOD_NAME;           
            
          	System.out.println("deleteFileById Address : " + Address);
          	System.out.println("deleteFileById SOAP_ACTION : " + SOAP_ACTION);
          	System.out.println("deleteFileById URL : " + URL);
          	System.out.println("MainActivity.address.toString() : " + MainActivity.address.toString());
          	
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            request.addProperty("fileId", fileId);            
            request.addProperty("comment", comment);
            
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.setOutputSoapObject(request);
            AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);              
            androidHttpTransport.call(SOAP_ACTION, envelope);                               
            SoapPrimitive result = (SoapPrimitive) envelope.getResponse();          
            String myResult = result.toString().trim();
            return myResult;
        }
        
        public static String writeUserLog(int fileID, int action, String note) throws Exception {
        	
        	Address = MainActivity.address.toString();
            String METHOD_NAME = "writeUserLog";
    		String SOAP_ACTION = "http://"+ Address +"/ServerWeb/services/Service/";
    		String URL = 		"http://"+ Address +"/ServerWeb/services/Service?wsdl";
            SOAP_ACTION = SOAP_ACTION + METHOD_NAME;            
            
          	System.out.println("writeUserLog Address : " + Address);
          	System.out.println("writeUserLog SOAP_ACTION : " + SOAP_ACTION);
          	System.out.println("writeUserLog URL : " + URL);          	
            
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            request.addProperty("fileID", fileID);
            request.addProperty("action", action);
            request.addProperty("note", note);

            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.setOutputSoapObject(request);
            AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);              
            androidHttpTransport.call(SOAP_ACTION, envelope);                               
            SoapPrimitive result = (SoapPrimitive) envelope.getResponse();          
            String myResult = result.toString().trim();
            return myResult;            
        }                    

}
